<!-- 培训卡片 -->
<template>
  <div class="trainingCard" @click="toDetails">
    <div class="cardBanner">
      <img
        class="cardImg"
        :src="$getImageUrl(useCardData.originImg)"
        alt=""
        srcset=""
      />
      <!-- <div class="cardTag">名师面对面</div> -->
    </div>
    <div class="cardInfo">
      <div class="cardTitle oneHidden">{{ useCardData.name }}</div>
      <div class="cardTime">
        <img
          class="calendarIcon"
          src="@/assets/calendar-icon.png"
          alt=""
          srcset=""
        />
        {{ formatDate(useCardData.beginTime) }}
      </div>
      <div class="cardOther">
        <div class="lecturer">讲师：{{ useCardData.promoter || '无' }}</div>
        <div class="otherRgt">
          <div class="booking">{{ useCardData.joins || 0 }}人已预约</div>
          <CustomButton v-if="useCardData.status === 'NO_START'" :disabled="useCardData.join" type="primary" plain @click="joinTeachFn">{{ useCardData.join ? '已预约' : '预约直播' }}</CustomButton>
          <CustomButton v-if="useCardData.status === 'START'" type="primary" plain>观看直播</CustomButton>
          <CustomButton v-if="useCardData.status === 'END'" type="default">查看回放</CustomButton>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import { toLogin } from '@/utils/baseConfig';
import CustomButton from './CustomButton.vue';
import { joinTeach } from '@/api/teach';

export default {
  name: 'TrainingCard',
  components: { CustomButton },
  props: {
    cardData: {
      type: Object,
      default: () => {
        return {
          name: '培训名称',
          beginTime: '2024/12/12 12:12:12',
          speaker: '讲师'
        }
      },
    },
  },
  data() {
    return {};
  },
  computed: {
    isLogin() {
      return !!getToken();
    },
    useCardData() {
      return JSON.parse(JSON.stringify(this.cardData));
    }
  },
  methods: {
    toDetails() {
      const url = this.$router.resolve({
        path: '/trainingDetails',
        query: {
          id: this.useCardData.objectId
        }
      }).href;
      window.open(url, '_blank');
    },
    formatDate(value) {
      return value ? this.$dayjs(value).format('YYYY/MM/DD HH:mm') : '';
    },
    // 预约直播
    async joinTeachFn() {
      event.stopPropagation();
      if(!this.isLogin) {
        return toLogin();
      }
      try {
        const params = {
          objectId: this.useCardData.objectId,
        };
        await joinTeach(params);
        this.$emit('updateData');
        this.$message.success('预约成功');
      } catch (error) {
        console.error(error);
      }
    },
  },
};
</script>

<style scoped lang="scss">
.trainingCard {
  width: 326px;
  height: 305px;
  border-radius: 8px;
  box-shadow: $box-shadow;
  background: #fff;
  &:hover {
    box-shadow: $box-shadow-hover;
  }

  .cardBanner {
    width: 100%;
    height: 196px;
    position: relative;
    background: #fff;
    overflow: hidden;
    display: flex;
    align-items: center;
    border-radius: 8px 8px 0 0;
    .cardImg {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    .cardTag {
      width: 130px;
      height: 30px;
      border-radius: 8px 0px 10px 0px;
      background: linear-gradient(99.85deg, #f34242 0%, #ff763b 100%);
      display: flex;
      align-items: center;
      justify-content: center;
      color: #fff;
      position: absolute;
      top: 0;
      left: 0;
    }
  }
  .cardInfo {
    padding: 12px 16px 0;
    .cardTitle {
      width: 100%;
      height: 21px;
      font-size: 14px;
      font-weight: 600;
      line-height: 21px;
      color: $text-title-color;
    }
    .cardTime {
      margin-top: 4px;
      font-size: 12px;
      font-weight: 400;
      height: 18px;
      display: flex;
      align-items: center;

      .calendarIcon {
        width: 12px;
        height: 12px;
        margin-left: 2px;
        margin-right: 6px;
      }
    }
    .cardOther {
      display: flex;
      align-items: center;
      margin-top: 12px;
      justify-content: space-between;
      .lecturer {
        height: 18px;
        line-height: 18px;
        font-size: 12px;
        font-weight: 400;
        color: $text-color;
      }
      .otherRgt {
        display: flex;
        align-items: center;
        justify-content: flex-end;
      }
      .booking {
        font-size: 10px;
        font-weight: 400;
        line-height: 15px;
        margin-right: 12px;
        color: $text-color-primary;
      }
    }
  }
}
</style>
